import data
import numpy as np
import matplotlib.pyplot as plt


# Using Barchart #
def autolabel(barrects, xpos='center'):
    xpos = xpos.lower()
    ha = {'center': 'center', 'right': 'left', 'left': 'right'}
    offset = {'center': 0.5, 'right': 0.57, 'left': 0.43}

    for rect in barrects:
        height = rect.get_height()
        axs.text(rect.get_x() + rect.get_width() * offset[xpos], 1.01 * height,
                '{}'.format(height), ha=ha[xpos], va='bottom')


def barsub(subnum):
    rects1 = axs[subnum].bar(ind - width, DependatV[title[subnum][0]], width, yerr=0,
                             color='SkyBlue', label=title[subnum][0])
    axs[subnum].set_ylabel(title[subnum][0], fontsize=16, family='Times New Roman')
    axs[subnum].set_title(title[subnum][0] + ' corresponding to input impedance', fontsize=16, family='Times New Roman')
    axs[subnum].set_xticks(ind)
    axs[subnum].set_xticklabels(Group, fontsize=16, family='Times New Roman')
    axs[subnum].legend()
    return rects1

plt.close()

SamplingF = 2 * 10 ** 9  # sampling frequency
Signal = []  # list to save Class_signal
DependatV = {}  # dependent variables. calculated
# select file and read csv
S, Pathname = data.readcsv()
for i in Pathname:
    Sdata, P = data.readcsv(i)
    Signal.append(data.signal(Sdata, SamplingF))
    # test 1 corresponding to energy
    # !!! need modification for different test !!!
    S_RMS, S_var, S_skew, S_kurtosis = Signal[-1].parameters_Nth(2)
    DependatV.setdefault('Kurtosis', []).append(S_kurtosis)  # index here means the ith signal in csv
    DependatV.setdefault('RMS', []).append(S_RMS)
    DependatV.setdefault('Variance', []).append(S_var)

print(Pathname)
Signal[1].mapsignal()

# print(DependatV)
title = [
    ['Kurtosis', 'Vpp/RMS'],
    ['RMS', 'RMS'],
    ['Variance', 'Var']]  # Consistent with the previous dictionary definition

print(DependatV[title[0][0]])

Group = ['1Mr', '50r', 'AC', 'default']  # according to pathname
ind = np.arange(len(Group))  # x location for the groups
width = 0.35  # width of one bar
fig, axs = plt.subplots(1, 3)
rects = []
for i in range(len(title)):
    print(i)
    rects.append(barsub(i))
plt.show()


